﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_PublicDatum。
    /// </summary>
    public class House_PublicDatum
    {
        public House_PublicDatum()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid HouseId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_PublicDatum where HouseId=@HouseId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, HouseId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_PublicDatum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_PublicDatum(");
            strSql.Append("HouseId,Community,BuildingName,Region,Block,Room,CommunityId,HouseAddress,HouseUse,BuildingType,BuildingArea,BuildingId,CustomerName,Mobile,Phone,Nationality,AddressZip,District,IsTransformed,AddPersonId,AddPerson,AddDepartId,AddDepart,CompanyId,CompanyName,Sex,CertificateType,CertificateNo,DemandType,CustomerType,CustomerAddress,Adddate)");

            strSql.Append(" values (");
            strSql.Append("@HouseId,@Community,@BuildingName,@Region,@Block,@Room,@CommunityId,@HouseAddress,@HouseUse,@BuildingType,@BuildingArea,@BuildingId,@CustomerName,@Mobile,@Phone,@Nationality,@AddressZip,@District,@IsTransformed,@AddPersonId,@AddPerson,@AddDepartId,@AddDepart,@CompanyId,@CompanyName,@Sex,@CertificateType,@CertificateNo,@DemandType,@CustomerType,@CustomerAddress,@Adddate)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "Community", DbType.AnsiString, model.Community);
            db.AddInParameter(dbCommand, "BuildingName", DbType.AnsiString, model.BuildingName);
            db.AddInParameter(dbCommand, "Region", DbType.AnsiString, model.Region);
            db.AddInParameter(dbCommand, "Block", DbType.AnsiString, model.Block);
            db.AddInParameter(dbCommand, "Room", DbType.AnsiString, model.Room);
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "HouseUse", DbType.AnsiString, model.HouseUse);
            db.AddInParameter(dbCommand, "BuildingType", DbType.AnsiString, model.BuildingType);
            db.AddInParameter(dbCommand, "BuildingArea", DbType.Double, model.BuildingArea);
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, model.BuildingId);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString, model.Phone);
            db.AddInParameter(dbCommand, "Nationality", DbType.AnsiString, model.Nationality);
            db.AddInParameter(dbCommand, "AddressZip", DbType.AnsiString, model.AddressZip);
            db.AddInParameter(dbCommand, "District", DbType.AnsiString, model.District);
            db.AddInParameter(dbCommand, "IsTransformed", DbType.Boolean, model.IsTransformed);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Sex", DbType.AnsiString, model.Sex);
            db.AddInParameter(dbCommand, "CertificateType", DbType.AnsiString, model.CertificateType);
            db.AddInParameter(dbCommand, "CertificateNo", DbType.AnsiString, model.CertificateNo);
            db.AddInParameter(dbCommand, "DemandType", DbType.Int32, model.DemandType);
            db.AddInParameter(dbCommand, "CustomerType", DbType.AnsiString, model.CustomerType);
            db.AddInParameter(dbCommand, "CustomerAddress", DbType.AnsiString, model.CustomerAddress);
            db.AddInParameter(dbCommand, "Adddate", DbType.DateTime, model.Adddate);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_PublicDatum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_PublicDatum set ");
            strSql.Append("Community=@Community,");
            strSql.Append("BuildingName=@BuildingName,");
            strSql.Append("Region=@Region,");
            strSql.Append("Block=@Block,");
            strSql.Append("Room=@Room,");
            strSql.Append("CommunityId=@CommunityId,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("HouseUse=@HouseUse,");
            strSql.Append("BuildingType=@BuildingType,");
            strSql.Append("BuildingArea=@BuildingArea,");
            strSql.Append("BuildingId=@BuildingId,");
            strSql.Append("CustomerName=@CustomerName,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Nationality=@Nationality,");
            strSql.Append("AddressZip=@AddressZip,");
            strSql.Append("District=@District,");
            strSql.Append("IsTransformed=@IsTransformed,");
            strSql.Append("AddPersonId=@AddPersonId,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("AddDepartId=@AddDepartId,");
            strSql.Append("AddDepart=@AddDepart,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("Sex=@Sex,");
            strSql.Append("CertificateType=@CertificateType,");
            strSql.Append("CertificateNo=@CertificateNo,");
            strSql.Append("DemandType=@DemandType,");
            strSql.Append("CustomerType=@CustomerType,");
            strSql.Append("CustomerAddress=@CustomerAddress,");
            strSql.Append("Adddate=@Adddate");
            strSql.Append(" where HouseId=@HouseId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "Community", DbType.AnsiString, model.Community);
            db.AddInParameter(dbCommand, "BuildingName", DbType.AnsiString, model.BuildingName);
            db.AddInParameter(dbCommand, "Region", DbType.AnsiString, model.Region);
            db.AddInParameter(dbCommand, "Block", DbType.AnsiString, model.Block);
            db.AddInParameter(dbCommand, "Room", DbType.AnsiString, model.Room);
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "HouseUse", DbType.AnsiString, model.HouseUse);
            db.AddInParameter(dbCommand, "BuildingType", DbType.AnsiString, model.BuildingType);
            db.AddInParameter(dbCommand, "BuildingArea", DbType.Double, model.BuildingArea);
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, model.BuildingId);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString, model.Phone);
            db.AddInParameter(dbCommand, "Nationality", DbType.AnsiString, model.Nationality);
            db.AddInParameter(dbCommand, "AddressZip", DbType.AnsiString, model.AddressZip);
            db.AddInParameter(dbCommand, "District", DbType.AnsiString, model.District);
            db.AddInParameter(dbCommand, "IsTransformed", DbType.Boolean, model.IsTransformed);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepart", DbType.AnsiString, model.AddDepart);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "Sex", DbType.AnsiString, model.Sex);
            db.AddInParameter(dbCommand, "CertificateType", DbType.AnsiString, model.CertificateType);
            db.AddInParameter(dbCommand, "CertificateNo", DbType.AnsiString, model.CertificateNo);
            db.AddInParameter(dbCommand, "DemandType", DbType.Int32, model.DemandType);
            db.AddInParameter(dbCommand, "CustomerType", DbType.AnsiString, model.CustomerType);
            db.AddInParameter(dbCommand, "CustomerAddress", DbType.AnsiString, model.CustomerAddress);
            db.AddInParameter(dbCommand, "Adddate", DbType.DateTime, model.Adddate);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid HouseId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_PublicDatum ");
            strSql.Append(" where HouseId=@HouseId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, HouseId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_PublicDatum GetModel(Guid HouseId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HouseId,Community,BuildingName,Region,Block,Room,CommunityId,HouseAddress,HouseUse,BuildingType,BuildingArea,BuildingId,CustomerName,Mobile,Phone,Nationality,AddressZip,District,IsTransformed,AddPersonId,AddPerson,AddDepartId,AddDepart,CompanyId,CompanyName,Sex,CertificateType,CertificateNo,DemandType,CustomerType,CustomerAddress,Adddate from House_PublicDatum ");
            strSql.Append(" where HouseId=@HouseId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, HouseId);
            Chenxin.Lib.House.Entity.House_PublicDatum model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HouseId,Community,BuildingName,Region,Block,Room,CommunityId,HouseAddress,HouseUse,BuildingType,BuildingArea,BuildingId,CustomerName,Mobile,Phone,Nationality,AddressZip,District,IsTransformed,AddPersonId,AddPerson,AddDepartId,AddDepart,CompanyId,CompanyName,Sex,CertificateType,CertificateNo,DemandType,CustomerType,CustomerAddress,Adddate ");
            strSql.Append(" FROM House_PublicDatum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);

            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "House_PublicDatum");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_PublicDatum> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HouseId,Community,BuildingName,Region,Block,Room,CommunityId,HouseAddress,HouseUse,BuildingType,BuildingArea,BuildingId,CustomerName,Mobile,Phone,Nationality,AddressZip,District,IsTransformed,AddPersonId,AddPerson,AddDepartId,AddDepart,CompanyId,CompanyName,Sex,CertificateType,CertificateNo,DemandType,CustomerType,CustomerAddress,Adddate ");
            strSql.Append(" FROM House_PublicDatum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_PublicDatum> list = new List<Chenxin.Lib.House.Entity.House_PublicDatum>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere, string col)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select {0} ", col);
            strSql.Append(" FROM House_PublicDatum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.AppendFormat(" group by {0}", col);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_PublicDatum ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_PublicDatum model = new Chenxin.Lib.House.Entity.House_PublicDatum();
            object ojb;
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.Community = dataReader["Community"].ToString();
            model.BuildingName = dataReader["BuildingName"].ToString();
            model.Region = dataReader["Region"].ToString();
            model.Block = dataReader["Block"].ToString();
            model.Room = dataReader["Room"].ToString();
            ojb = dataReader["CommunityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CommunityId = new Guid(ojb.ToString());
            }
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            model.HouseUse = dataReader["HouseUse"].ToString();
            model.BuildingType = dataReader["BuildingType"].ToString();
            ojb = dataReader["BuildingArea"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BuildingArea = Convert.ToDecimal(ojb);
            }
            ojb = dataReader["BuildingId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BuildingId = new Guid(ojb.ToString());
            }
            model.CustomerName = dataReader["CustomerName"].ToString();
            model.Mobile = dataReader["Mobile"].ToString();
            model.Phone = dataReader["Phone"].ToString();
            model.Nationality = dataReader["Nationality"].ToString();
            model.AddressZip = dataReader["AddressZip"].ToString();
            model.District = dataReader["District"].ToString();
            ojb = dataReader["IsTransformed"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsTransformed = (bool)ojb;
            }
            ojb = dataReader["AddPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddPersonId = new Guid(ojb.ToString());
            }
            model.AddPerson = dataReader["AddPerson"].ToString();
            ojb = dataReader["AddDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddDepartId = new Guid(ojb.ToString());
            }
            model.AddDepart = dataReader["AddDepart"].ToString();
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            model.Sex = dataReader["Sex"].ToString();
            model.CertificateType = dataReader["CertificateType"].ToString();
            model.CertificateNo = dataReader["CertificateNo"].ToString();
            ojb = dataReader["DemandType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DemandType = (int)ojb;
            }
            model.CustomerType = dataReader["CustomerType"].ToString();
            model.CustomerAddress = dataReader["CustomerAddress"].ToString();
            ojb = dataReader["Adddate"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Adddate = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

